//
//  MTVideoInfoTableView.swift
//  MT
//
//  Created by 陈悦 on 2020/5/6.
//  Copyright © 2020 张强. All rights reserved.
//

import UIKit
import MTCategory
import YHPhotoKit

@objc
protocol MTInfoVideoTableViewVerificationTask: NSObjectProtocol {
    @objc optional func infoVideoTableView()
}

class MTVideoInfoTableView: UITableView {
    
    /// 认证代理
    weak var verificationTask: MTInfoVideoTableViewVerificationTask?
    
    /// 下一步 FooterView
    var footerView: MTInfoVerificationTableFooterView?
    
    /// 列表控制数组
    private var infoTypes: [UserProfileInfos] = [.video, .authVideo]
    
    /// 视频
    var videoPath: String? {
        didSet {
            reloadData()
        }
    }
    
    var selfCoverImage: UIImage? {
        didSet {
            reloadRow(at: IndexPath(row: 0, section: 0), with: .none)
        }
    }
    /// 认证视频
    var authVideoCover: UIImage? {
        didSet {
            reloadRow(at: IndexPath(row: 1, section: 0), with: .none)
        }
    }

    
    init(frame: CGRect) {
        super.init(frame: frame, style: .grouped)
        setup()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

// MARK: ------------------ 初始化 ----------------------
extension MTVideoInfoTableView {
    
    private func setup() {
        backgroundColor = UIColor.theme.background
        delegate = self
        dataSource = self
        separatorStyle = .none
        keyboardDismissMode = .onDrag
        contentInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
        
        for type in infoTypes {
            register(type.cellClass, forCellReuseIdentifier: String(describing: type.cellClass))
        }
    }
    
}

// MARK: ------------------ 所有代理 ----------------------
extension MTVideoInfoTableView: UITableViewDelegate {
    
}

// MARK: ------------------ UI ----------------------
extension MTVideoInfoTableView: UITableViewDataSource {
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return infoTypes.count
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return infoTypes[indexPath.row].cellHeight
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let type = infoTypes[indexPath.row]
        let c = tableView.dequeueReusableCell(withIdentifier: String(describing: type.cellClass), for: indexPath)

        switch type {
        case .video:
            guard let cell = c as? MTInfoUploadVideoCell else {
                break
            }
            cell.type = type
            cell.screenMode = 0
            cell.assetImage = selfCoverImage
//            cell.assetImage = videoFirstFrame(videoPath ?? "", size: CGSize(width: 165, height: 93))
            return cell
        
        case .authVideo:
            guard let cell = c as? MTInfoUploadAuthVideoCell else {
                break
            }
//            cell.delegate = self
            cell.type = type
            cell.assetImage = authVideoCover
//            cell.selectedType = gender
            return cell
            
        default: break
        }
        
        return c
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return CGFloat.leastNonzeroMagnitude
    }
    
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        if section == 0 {
            return 91
        }
        return CGFloat.leastNonzeroMagnitude
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        return nil
    }
    
    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        if section == 0 {
            let v = MTInfoVerificationTableFooterView(frame: CGRect(x: 0, y: 0, width: ScreenWidth, height: 91))
            v.delegate = self
            v.buttonStr = "完成"
            self.footerView = v
            return v
        }
        return nil
    }
    
}

// MARK: ------------------ FooterView 代理回调 ----------------------
extension MTVideoInfoTableView: MTInfoVerificationTableFooterViewDelegate {
    
    func tableFooterViewDidClickButton(_ footerView: MTInfoVerificationTableFooterView) {
        if let delegate = verificationTask {
            delegate.infoVideoTableView?()
        }
//        if !locallyVerifyInfos() { return }
//        guard let asset = imageAsset else { return }
//        if nickname?.utf8.count ?? 0 > UserProfileInfos.nickname.maximumTextCount ?? 0 {
//            AlertManager.presentAlertView(message: "昵称超出字数", duration: 1)
//            return
//        }
//        if let verificationTask = verificationTask, let nickname = nickname, let birthday = birthday, let location = location, gender != .unknown {
//            MTAssetLibrary.default.requestImage(withAsset: asset.asset, photoWidth: ScreenWidth, progressHandler: nil, errorHandler: nil) { [weak self] (image, info, isDegraded) in
//                guard let weakSelf = self, let image = image, let isDegraded = isDegraded else { return }
//                if !isDegraded {
//                    weakSelf.endEditing(true)
//                    verificationTask.infoVerificationTableView?(weakSelf, willSubmitInfosWithPortrait: image, nickname: nickname, gender: weakSelf.gender, birthday: birthday, location: location)
//                }
//            }
//        }
    }
    
}
